package Storage;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class LocationOverlay extends ItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private Paint innerPaint, borderPaint, textPaint;
	private OverlayItem onTapOverlayItem;

	public LocationOverlay(Drawable defaultMarker, Context context) {
		super(boundCenter(defaultMarker));
		mContext = context;
		populate();

	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	protected boolean onTap(int i) {
		onTapOverlayItem = mOverlays.get(i);
		return true;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		drawInfoWindow(canvas, mapView, shadow);
	}

	private void drawInfoWindow(Canvas canvas, MapView mapView, boolean shadow) {
		if (onTapOverlayItem != null) {

			// First determine the screen coordinates of the selected
			// MapLocation
			int INFO_WINDOW_HEIGHT = 0;
			Point onTapDestinationOffset = new Point();
			mapView.getProjection().toPixels(onTapOverlayItem.getPoint(),
					onTapDestinationOffset);

			// Setup the info window with the right size & location

			int INFO_WINDOW_WIDTH = Math.max(onTapOverlayItem.getTitle()
					.length(), onTapOverlayItem.getSnippet().length()) * 7;
			if (onTapOverlayItem.getSnippet().equals(""))
				INFO_WINDOW_HEIGHT = 20;
			else
				INFO_WINDOW_HEIGHT = 40;
			RectF infoWindowRect = new RectF(0, 0, INFO_WINDOW_WIDTH,
					INFO_WINDOW_HEIGHT);
			int infoWindowOffsetX = onTapDestinationOffset.x
					- INFO_WINDOW_WIDTH / 2;
			int infoWindowOffsetY = onTapDestinationOffset.y
					- INFO_WINDOW_HEIGHT - 15;
			infoWindowRect.offset(infoWindowOffsetX, infoWindowOffsetY);

			// Draw inner info window
			canvas.drawRoundRect(infoWindowRect, 5, 5, getInnerPaint());

			// Draw border for info window
			canvas.drawRoundRect(infoWindowRect, 5, 5, getBorderPaint());

			// Draw the MapLocation's name
			int TEXT_OFFSET_X = 5;
			int TEXT_OFFSET_Y = 15;
			// title
			canvas.drawText(onTapOverlayItem.getTitle(), infoWindowOffsetX
					+ TEXT_OFFSET_X, infoWindowOffsetY + TEXT_OFFSET_Y,
					getTextPaint());
			// snippet
			if (!onTapOverlayItem.getSnippet().equals("")) {
				canvas.drawText(onTapOverlayItem.getSnippet(),
						infoWindowOffsetX + TEXT_OFFSET_X, infoWindowOffsetY
								+ TEXT_OFFSET_Y + 15, getTextPaint());
			}

		}
	}

	public Paint getInnerPaint() {
		if (innerPaint == null) {
			innerPaint = new Paint();
			innerPaint.setARGB(225, 75, 75, 75); // gray
			innerPaint.setAntiAlias(true);
		}
		return innerPaint;
	}

	public Paint getBorderPaint() {
		if (borderPaint == null) {
			borderPaint = new Paint();
			borderPaint.setARGB(255, 255, 255, 255);
			borderPaint.setAntiAlias(true);
			borderPaint.setStyle(Style.STROKE);
			borderPaint.setStrokeWidth(2);
		}
		return borderPaint;
	}

	public Paint getTextPaint() {
		if (textPaint == null) {
			textPaint = new Paint();
			textPaint.setARGB(255, 255, 255, 255);
			textPaint.setAntiAlias(true);
		}
		return textPaint;
	}

}
